package com.lottery.ruok.lottrey.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.fragment.AptitudeFragment;
import com.lottery.ruok.lottrey.retrofitModule.response.IssueNoListResponse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class AptitudeAdapter extends BaseAdapter {
    private List<IssueNoListResponse> datas;
    private AptitudeFragment aptitudeFragment;

    public AptitudeAdapter(AptitudeFragment aptitudeFragment){
        this.aptitudeFragment = aptitudeFragment;
    }

    public void setData(List<IssueNoListResponse> e) {
        datas = e;
    }

    public List<IssueNoListResponse> getData() {
        return datas;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(aptitudeFragment.getActivity()).inflate(R.layout.aptitude_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.number.setText(datas.get(i).getIssue_no() + "");
        holder.time.setText(datas.get(i).getEnd_time());
        holder.money.setText(Utils.saveTwoFormat(datas.get(i).getMultiple() * aptitudeFragment.getArguments().getFloat("money")));
        holder.multiple.setText(datas.get(i).getMultiple() + "");

        final TextView temp = holder.multiple;
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//不能是空字符串
                if (temp.getText().toString().equals("")) {
                    datas.get(i).setMultiple(1);
                } else {
                    datas.get(i).setMultiple(Integer.parseInt(temp.getText().toString()) + 1);
                }

                aptitudeFragment.setResults2();
            }
        });
        holder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//不能是空字符串
                if (!temp.getText().toString().equals("") && Integer.parseInt(temp.getText().toString()) > 1) {
                    datas.get(i).setMultiple(Integer.parseInt(temp.getText().toString()) - 1);

                    aptitudeFragment.setResults2();
                }
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.number)
        TextView number;
        @BindView(R.id.minus)
        ImageView minus;
        @BindView(R.id.add)
        ImageView add;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.multiple)
        TextView multiple;
        @BindView(R.id.money)
        TextView money;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }
}
